package aad.storyboard.model;


import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import aad.storyboard.controller.AudioPlayer;
import aad.storyboard.controller.StoryTimer;
import android.widget.RelativeLayout;


public class Story implements Observer{
	
	private static ArrayList <Chapter> chapters=new ArrayList <Chapter> ();	
	private static Chapter currentChapter;
	private static StoryTimer timer = null;
	private static AudioPlayer aPlayer;
	private StorySettings settings;
	private Image storyImage;

	
	private String title;
	private String description;
	private int audioFile;
	private static RelativeLayout gScene;
	
	private static Story inst=new Story();
	
	private Story (){
		settings = new StorySettings();
	}
	
	public static Story getInstance(){
		return inst;
	}
	
	public static void setTimer()
	{
		timer = StoryTimer.getInstance();
        timer.addObserver(inst); 
	}
	
	public static void setAudioPlayer()
	{
		aPlayer=AudioPlayer.getInstance();
	}
	
	public void setChapters(ArrayList <Chapter> chapters)
	{
	  Story.chapters=chapters;
	}
	
	public ArrayList <Chapter> getChapters()
	{
		return chapters;
	}
	
	public  void addChapter(Chapter chap)
	{
	chapters.add(chap);
	}
	
	public void setCurrentChapter(Chapter currentChapter) {
		Story.currentChapter = currentChapter;
		currentChapter.setCurrentScene(currentChapter.getScene(0));

	}

	public Chapter getCurrentChapter() {
		return currentChapter;
	}
	
	public Chapter getChapter(int i)
	{
		return chapters.get(i);
	}
	
	public Chapter getNextChapter()
	{
		int i=1;
		for (Chapter ch: chapters)
		{
			if (ch.equals(currentChapter)&&i<chapters.size())
			{
				System.out.println("Next chapter found");
				return chapters.get(i);
			}
			i++;
		}
		return null;
	}
	
	@Override
    public void update(Observable obs, Object obj) {        
        timer=(StoryTimer)obs;
        updateCurrentChapter(timer.getCurrentTime());             
    }

	public void setCurrentChapter(int i) {
		Story.currentChapter=chapters.get(i);
		currentChapter.setCurrentScene(currentChapter.getScene(0));
		
	}
	
	public void updateCurrentChapter(int time)
	{
	  
		for(Chapter c: chapters)
		{
			StoryComponentState old=c.getState();
			c.updateState(time);
			if (c.getState()!=old)//if current chapter has been updated
			{
				System.out.println("New Chapter set");
				currentChapter=c;
				break;
			}
		
		}
		
		updateCurrentScene(time);
		
	}	
	
	public void updateCurrentScene(int time)
	{
		
		for(Scene s: currentChapter.getScenes())
		{
			StoryComponentState old=s.getState();
			s.updateState(time);
			if (s.getState()!=old)//if current scene has been updated
			{
				System.out.println("New scene set");
				currentChapter.setCurrentScene(s);
				break;
			}
		
		}
	}
		
		
	public static void setgScene(RelativeLayout gScene) {
		Story.gScene = gScene;
	}

	public static RelativeLayout getgScene() {
		return gScene;
	}

	public String getTitle() {
		// TODO Auto-generated method stub
		return this.title;
	}
	
	public void setTitle(String t){
		this.title = t;
	}
	
	public String getDescription(){
		return this.description;
	}
	
	public void setDescription(String des){
		this.description = des;
	}
	

	public Image getStoryImage() {
		return storyImage;
	}

	public void setStoryImage(Image storyImage) {
		this.storyImage = storyImage;
	}

	public int getAudioFile() {
		return this.audioFile;
	}
	
	public void setAudioFile(int fileID){
		this.audioFile = fileID;
	}

	public StorySettings getSettings() {
		return settings;
	}

	public void setSettings(StorySettings settings) {
		this.settings = settings;
	}
	
	
    
}
